package com.neu.flashmall.product.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.common.utils.Result;
import com.neu.flashmall.product.entity.PmsSkuImages;
import com.neu.flashmall.product.entity.PmsSkuInfo;
import com.neu.flashmall.product.entity.PmsSkuSaleAttrValue;
import com.neu.flashmall.product.entity.PmsSpuInfo;
import com.neu.flashmall.product.feign.ThirdPartyFeignService;
import com.neu.flashmall.product.mapper.PmsSkuImagesMapper;
import com.neu.flashmall.product.mapper.PmsSkuInfoMapper;
import com.neu.flashmall.product.mapper.PmsSkuSaleAttrValueMapper;
import com.neu.flashmall.product.mapper.PmsSpuInfoMapper;
import com.neu.flashmall.product.service.PmsSkuInfoService;
import com.neu.flashmall.product.vo.GoodVo;
import com.neu.flashmall.product.vo.SkuGoodVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PmsSkuInfoServiceImpl implements PmsSkuInfoService {

    @Resource
    private PmsSkuInfoMapper pmsSkuInfoMapper;

    @Resource
    private ThirdPartyFeignService thirdPartyFeignService;

    @Resource
    private PmsSpuInfoMapper pmsSpuInfoMapper;

    @Resource
    private PmsSkuSaleAttrValueMapper pmsSkuSaleAttrValueMapper;

    @Resource
    private PmsSkuImagesMapper pmsSkuImagesMapper;


    @Override
    public PageInfo<PmsSkuInfo> getPaged(int pageNum, int pageSize, long spuId) {
        PageHelper.startPage(pageNum,pageSize);
        List<PmsSkuInfo> skuList = pmsSkuInfoMapper.selectAll(spuId);

        PageInfo<PmsSkuInfo> pageInfo = new PageInfo<>(skuList);

        return pageInfo;
    }

    @Override
    public PageInfo<PmsSkuInfo> selectBySkuName(String skuName,int pageNum, int pageSize, long spuId) {
        PageHelper.startPage(pageNum,pageSize);
        List<PmsSkuInfo> skuList = pmsSkuInfoMapper.selectBySkuName("%"+skuName+"%",spuId);

        PageInfo<PmsSkuInfo> pageInfo = new PageInfo<>(skuList);
        return pageInfo;
    }

    @Override
    public String delete(Long skuId) {
        Result result = thirdPartyFeignService.deleteSkuAllImage(String.valueOf(skuId));
        if(result.getCode() == 2000){
            pmsSkuImagesMapper.deleteBySkuId(skuId);
            pmsSkuSaleAttrValueMapper.deleteBySkuId(skuId);
            int delete_num = pmsSkuInfoMapper.deleteByPrimaryKey(skuId);
            if(delete_num == 1) return "删除成功";
            return "数据库异常";
        }
        return "云服务器删除异常";
    }

    @Override
    public String insert(PmsSkuInfo pmsSkuInfo) {
        pmsSkuInfo.setSaleCount(0L);
        int insert_num = pmsSkuInfoMapper.insert(pmsSkuInfo);
        if(insert_num > 0){
            return "添加成功";
        }
        return "";
    }

    @Override
    public String update(PmsSkuInfo pmsSkuInfo) {
        int update_num = pmsSkuInfoMapper.updateByPrimaryKeySelective(pmsSkuInfo);
        if(update_num > 0){
            return "修改成功";
        }
        return "";
    }

    @Override
    public String deleteByList(List<Long> idList) {
        int len = idList.size();
        int all = 0;
        for(Long id : idList){
            if(delete(id).equals("删除成功")){
                all++;
            }
        }
        if(all == len) return "删除成功";
        return "删除失败";
    }

    @Override
    public BigDecimal getPrice(Long spuId) {
        return this.pmsSkuInfoMapper.getPrice(spuId);
    }

    @Override
    public List<SkuGoodVo> getAllSkuGood(Long id){
        List<SkuGoodVo> result = new ArrayList<>();
        List<PmsSkuInfo> all = pmsSkuInfoMapper.selectAllBySpuId(id);
        for(PmsSkuInfo sku : all){
            List<PmsSkuSaleAttrValue> attrList = pmsSkuSaleAttrValueMapper.getBySkuId(sku.getId());
            String attr = "";
            for (PmsSkuSaleAttrValue pmsSkuSaleAttrValue : attrList) {
                attr += pmsSkuSaleAttrValue.getAttrName() + ": " + pmsSkuSaleAttrValue.getAttrValue() + "  ";
            }
            List<PmsSkuImages> images = pmsSkuImagesMapper.getAllBySkuId(sku.getId());
            SkuGoodVo item = new SkuGoodVo();
            item.setDetail(sku);
            item.setId(sku.getId());
            item.setPrice(sku.getPrice());
            item.setName(sku.getSkuTitle());
            item.setSkuDesc(sku.getSkuSubtitle());
            if(images != null && images.size() != 0){
                List<String> imageList = new ArrayList<>();
                for(PmsSkuImages i : images){
                    imageList.add(i.getImg());
                    item.setAllImage(imageList);
                }
            }
            item.setAttrsText(attr);
            result.add(item);
        }
        return result;
    }

    @Override
    public GoodVo getGoodVo(Long id){
        GoodVo goodVo = new GoodVo();
        PmsSpuInfo pmsSpuInfo = pmsSpuInfoMapper.selectByPrimaryKey(id);
        goodVo.setSpuInfo(pmsSpuInfo);
        List<GoodVo.AttrItem> attrItemList = new ArrayList<>();
        List<PmsSkuInfo> skuInfoList = pmsSkuInfoMapper.selectAllBySpuId(id);
        HashMap<Long, GoodVo.AttrItem> map = new HashMap<>();
        for(PmsSkuInfo sku : skuInfoList){
            List<PmsSkuSaleAttrValue> attrValues = pmsSkuSaleAttrValueMapper.getBySkuId(sku.getId());
            for(PmsSkuSaleAttrValue value : attrValues){
                GoodVo.AttrItem item = null;
                if(map.containsKey(value.getAttrId())){
                    item = map.get(value.getAttrId());
                }else{
                    item = new GoodVo.AttrItem();
                    item.setAttrName(value.getAttrName());
                    item.setAttrId(value.getAttrId());
                    item.setOptions(new ArrayList<>());
                    attrItemList.add(item);
                    map.put(value.getAttrId(),item);
                }
                List<GoodVo.SkuItem> options = item.getOptions();
                int flag = 0;
                for(GoodVo.SkuItem o : options){
                    if(o.getAttrValue().equals(value.getAttrValue())){
                        flag = 1;
                        o.getSkuId().add(sku.getId());
                        break;
                    }
                }
                if(flag == 0){
                    GoodVo.SkuItem newOption = new GoodVo.SkuItem();
                    newOption.setDisabled(false);
                    newOption.setSelected(false);
                    newOption.setAttrValue(value.getAttrValue());
                    newOption.setSkuId(new ArrayList<>());
                    newOption.getSkuId().add(sku.getId());
                    options.add(newOption);
                }
            }
        }
        goodVo.setAttrList(attrItemList);
        return goodVo;
    }

    @Override
    public List<Long> searchByKeyWord(String keyWord) {
        return pmsSpuInfoMapper.searchByKeyWord("%" + keyWord + "%");
    }
}
